package mysql

import (
	"engine/log"
	"gorm.io/gorm"

	"sync/atomic"
)

var _gMysqlCli *cli
var _gMysqlInstCount atomic.Int64

func Start() (c *gorm.DB, err error) {
	if n := _gMysqlInstCount.Add(1); n == 1 {
		_gMysqlCli = &cli{}
		log.GetLogger().Info("start mysql").Println()
		err = _gMysqlCli.start()
	}
	return _gMysqlCli.db, err
}

func Stop() {
	old := _gMysqlCli
	if n := _gMysqlInstCount.Add(-1); n == 0 {
		old.stop()
		log.GetLogger().Info("stop mysql").Println()
	}
}

func GetInstance() *gorm.DB {
	return _gMysqlCli.db
}
